<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\Request;
use app\admin\model\Node as NoModel;

class Posts extends Base
{
    protected $middleware = [
        '\app\admin\middleware\LoginCheck',
        '\app\admin\middleware\UrlData',
        '\app\admin\middleware\UrlCheck'

    ];

    //帖子控制器

    public function index() //帖子列表
    {
       return view();
    }

    public function data($limit,$page,Request $request)  //帖子表数据
    {
        $offset = ($page-1)*$limit;
        $limit = (int)$limit;
        $user = $request->user;

        $list = Db::table('posts')
            ->alias('p')
            ->leftJoin('post_cate p_c','p.cate_id=p_c.id')
            ->where('p.user_id',$user['id'])
            ->where('p.status','<>',2)
            ->field('p.id,p.post_title,p.post_img,p_c.cate_name,p.status,p.views,p.create_time,p.update_time')
            ->limit($offset,$limit)
            ->select()->toArray();

        $count = Db::table('posts')->where('user_id',$user['id'])->count();

        return json([
            "code"=>0,
            "msg"=>"",
            "count"=>$count,
            "data"=>$list
        ]);
    }

    public function update(Request $request)    //帖子的修改
    {
        if($this->request->isPost()){   //如果是post方式提交
            $data = $this->request->post();
            $user = $request->user;
            $data['user_id'] = $user['id'];

            $post_img = $this->haveUpFile('post_img',$_FILES);
            if($post_img){
                $data['post_img'] = $post_img;
            }

            $data['update_time'] = date("Y-m-d H:i:s");
            $res = Db::table('posts')->update($data);

            if($res){
                $this->success('文章修改成功','index');
            }
            $this->error('文章修改失败');
        }


        $id = $this->request->get('id');    //获取文章id

        $cate = Db::table('post_cate')->field('id,cate_name')->select();
        $user_post = Db::table('posts')     //查询文章信息
            ->where('id',$id)
            ->field('id,post_title,post_img,post_content,cate_id,`status`,views')
            ->find();

        return view('add',['cate'=>$cate,'user_posts'=>$user_post]);
    }

    public function add(Request $request)   //帖子添加
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $user = $request->user;
            $data['user_id'] = $user['id'];

            $have = $_FILES;


            if(!empty($have["post_img"]["name"])){
                $file = request()->file('post_img');
                $post_img = \think\facade\Filesystem::disk('public')->putFile( 'post_img', $file);
                $post_img = "/storage/".$post_img;

                $data['post_img'] = $post_img;
            }

            $data['views'] = 0; //浏览量初始为0

            $data = $this->insertTime($data);

            $res = Db::table('posts')->save($data);
            if($res){
                $this->success('文章发布成功','index');
            }
            $this->error('文章发布失败，请重试');
        }

        $cate = Db::table('post_cate')->field('id,cate_name')->select();

       return view('',['cate'=>$cate]);
    }

    public function delete()    //帖子删除
    {
        if($this->request->isPost()){
            $id = $this->request->post('id');

            $res = Db::table('posts')->where('id',$id)->update(['status' => '2']);

            if($res){
                return json([
                    'code'=>200,
                    'msg'=>'删除成功'
                ]);
            }
            return json([
                'code'=>400,
                'msg'=>'删除失败'
            ]);
        }
    }


}
